package com.ruoyi.index.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.index.domain.TodoInfo;
import com.ruoyi.index.service.ITodoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 代办Controller
 *
 * @author ruoyi
 * @date 2022-09-08
 */
@RestController
@RequestMapping("/index/TodoInfo")
public class TodoInfoController extends BaseController {
    @Autowired
    private ITodoInfoService todoInfoService;

    /**
     * 查询代办列表
     */
    @PreAuthorize("@ss.hasPermi('index:TodoInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(TodoInfo todoInfo) {
        startPage();
        List<TodoInfo> list = todoInfoService.selectTodoInfoList(todoInfo);
        return getDataTable(list);
    }

    /**
     * 导出代办列表
     */
    @PreAuthorize("@ss.hasPermi('index:TodoInfo:export')")
    @Log(title = "代办", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TodoInfo todoInfo) {
        List<TodoInfo> list = todoInfoService.selectTodoInfoList(todoInfo);
        ExcelUtil<TodoInfo> util = new ExcelUtil<TodoInfo>(TodoInfo.class);
        util.exportExcel(response, list, "代办数据");
    }

    /**
     * 获取代办详细信息
     */
    @PreAuthorize("@ss.hasPermi('index:TodoInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(todoInfoService.selectTodoInfoById(id));
    }

    /**
     * 新增代办
     */
    @PreAuthorize("@ss.hasPermi('index:TodoInfo:add')")
    @Log(title = "代办", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TodoInfo todoInfo) {
        return toAjax(todoInfoService.insertTodoInfo(todoInfo));
    }

    /**
     * 修改代办
     */
    @PreAuthorize("@ss.hasPermi('index:TodoInfo:edit')")
    @Log(title = "代办", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TodoInfo todoInfo) {
        return toAjax(todoInfoService.updateTodoInfo(todoInfo));
    }

    /**
     * 删除代办
     */
    @PreAuthorize("@ss.hasPermi('index:TodoInfo:remove')")
    @Log(title = "代办", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(todoInfoService.deleteTodoInfoByIds(ids));
    }


    /**
     * 获取当前的人代办事项
     */
    @GetMapping("/mattersList")
    public TableDataInfo mattersList(TodoInfo todoInfo) {
        todoInfo.setStatus("0");
        List<SysRole> roles = getLoginUser().getUser().getRoles();
        Long[] roleIds = new Long[roles.size()];
        for (int i = 0; i < roles.size(); i++) {
            roleIds[i] = roles.get(i).getRoleId();
        }
        todoInfo.setRoleIds(roleIds);
        List<TodoInfo> list = todoInfoService.selectTodoInfoList(todoInfo);
        return getDataTable(list);
    }
    /**
     * 获取我的发起
     */
    @GetMapping("/initiationList")
    public TableDataInfo initiationList(TodoInfo todoInfo) {
        todoInfo.setCreateBy(getUsername());
        List<TodoInfo> list = todoInfoService.selectTodoInfoList(todoInfo);
        return getDataTable(list);
    }
}
